/*
 * File:         TriperestUserTest.java
 * Author:       Robert Bittle <guywithnose@gmail.com>
 */
package triperest.tests;

import static org.junit.Assert.*;

import java.util.HashMap;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import tests.AppEngineTest;
import tests.HttpServletRequestMock;
import triperest.Trip;
import triperest.TriperestUser;

/**
 * The Class TriperestUserTest.
 */
public class TriperestUserTest extends AppEngineTest {

	/** The user. */
	private TriperestUser user;
	
	private HttpServletRequestMock mockReq = new HttpServletRequestMock(new HashMap<String, String>(), new HashMap<String, String>());
	
	/* (non-Javadoc)
	 * @see tests.AppEngineTest#setUp()
	 */
	@Before
	@Override
	public void setUp()
	{
		super.setUp();
		user = new TriperestUser(mockReq);
		user.setName("testName");
		user.delete();
	}
	
	/* (non-Javadoc)
	 * @see tests.AppEngineTest#tearDown()
	 */
	@Override
	@After
	public void tearDown()
	{
		user.delete();
		super.tearDown();
	}
	
	/**
	 * Tests the save method using insert.
	 */
	@Test
	public void testSave_Insert() {
		user.save();
		TriperestUser user2 = new TriperestUser(mockReq);
		user2.load();
		assertEquals(user.getId(), user2.getId());
		assertEquals(user.getName(), user2.getName());
	}
	
	/**
	 * Tests the save method using update.
	 */
	@Test
	public void testSave_Update() {
		user.save();
		user.setName("Another Test Name");
		user.save();
		TriperestUser user2 = new TriperestUser(mockReq);
		user2.load();
		assertEquals(user.getId(), user2.getId());
		assertEquals(user.getName(), user2.getName());
	}
	
	/**
	 * Tests the delete method.
	 */
	@Test
	public void testDelete() {
		user.save();
		assertTrue(user.load());
		user.delete();
		assertFalse(user.load());
	}
	
	/**
	 * Tests the save method using insert trips.
	 */
	@Test
	public void testSave_InsertTrips() {
		Trip trip = new Trip();
		trip.setDescription("A test Trip");
		trip.setName("My trip");
		user.addTrip(trip);
		user.save();
		TriperestUser user2 = new TriperestUser(mockReq);
		user2.load();
		assertEquals(user.getId(), user2.getId());
		assertEquals(user.getName(), user2.getName());
		assertEquals(user.getTrips().get(0).getDescription(), user2.getTrips().get(0).getDescription());
		assertEquals(user.getTrips().get(0).getName(), user2.getTrips().get(0).getName());
		assertEquals(user.getTrips().get(0).getId(), user2.getTrips().get(0).getId());
		trip.delete();
	}
	
	/**
	 * Tests the save method using update trips.
	 */
	@Test
	public void testSave_UpdateTrips() {
		user.save();
		Trip trip = new Trip();
		trip.setDescription("A test Trip");
		trip.setName("My trip");
		user.addTrip(trip);
		user.save();
		user.getTrips().get(0).setDescription("abcdefg");
		user.getTrips().get(0).setName("ggffddssaa");
		user.save();
		TriperestUser user2 = new TriperestUser(mockReq);
		user2.load();
		assertEquals(user.getId(), user2.getId());
		assertEquals(user.getName(), user2.getName());
		assertEquals(user.getTrips().get(0).getDescription(), user2.getTrips().get(0).getDescription());
		assertEquals(user.getTrips().get(0).getName(), user2.getTrips().get(0).getName());
		assertEquals(user.getTrips().get(0).getId(), user2.getTrips().get(0).getId());
		trip.delete();
	}

}
